package com.baitiaojun.user.api.impl;

import com.baitiaojun.api.annotation.JSR303Verify;
import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.user.FollowingGroup;
import com.baitiaojun.api.domain.user.UserFollowing;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.user.api.UserFollowingApi;
import com.baitiaojun.user.service.FollowingGroupService;
import com.baitiaojun.user.service.UserFollowingService;
import com.baitiaojun.user.support.UserSupport;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @类名 UserFollowingController
 * @描述:获取关注的其他用户、关注其他用户、更改用户分组、取消关注其他用户、获取用户分组、创建用户分组、删除用户分组、获取用户粉丝
 * @作者 白条君
 * @创建日期 2022/10/29 22:01
 * @版本 1.0
 */
@RestController
public class UserFollowingApiImpl implements UserFollowingApi {

    @Resource
    private UserFollowingService userFollowingService;

    @Resource
    private UserSupport userSupport;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @Resource
    private FollowingGroupService followingGroupService;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes addUserFollowing(@Valid UserFollowing userFollowing, BindingResult result) throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        userFollowing.setUserid(currentUserId);
        String token = userSupport.getToken(request);
        userFollowingService.addAndUpdateUserFollowing(userFollowing, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes addFollowingGroup(@Valid FollowingGroup followingGroup, BindingResult result) throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        Long groupId = followingGroupService.addFollowingGroup(followingGroup.getName(), currentUserId, token, UserFollowing.builder().userid(currentUserId).build());
        response.setHeader("Authorization", token);
        return JsonRes.builder().success().data(groupId);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes getUserFollowingsWithGroup() throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        Object followingGroup = userFollowingService.getUserFollowings(token, UserFollowing.builder().userid(currentUserId).build());
        response.setHeader("Authorization", userSupport.getToken(request));
        return JsonRes.builder().success().data(followingGroup);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes getFollowingGroup() throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        Object followingGroupList = followingGroupService.getFollowingGroupByUserId(currentUserId, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success().data(followingGroupList);
    }

    @Override
    public Set<Long> getFollowingUserIds(Long userId) throws ConditionException {
        return userFollowingService.getFollowingUserIdsByUserId(userId);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes cancelUserFollowing(@Valid UserFollowing userFollowing, BindingResult result) throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        userFollowingService.cancelUserFollowing(currentUserId, userFollowing, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes cancelFollowingGroup(@Valid @RequestBody FollowingGroup followingGroup, BindingResult result) throws Exception {
        Long currentUserId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        followingGroupService.cancelFollowingGroup(followingGroup.getId(), currentUserId, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success();
    }
}